﻿/******************************************************    
文件名:TableTerminologyDB.cs
版权  (c)  2010-11-23 淘渺圈圈工作室
作者:黄媛媛
创建日期:2010-12-05
维护人员:黄媛媛
维护日期:2010-12-05
文件功能描述:金属材料热处理网络学习平台_访问terminology数据表操作
******************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Data;

namespace Mmht.DB
{
    /// <summary>
    /// 名词术语
    /// </summary>
    public class TableTerminologyDB
    {
        operate op = new operate();

        public DataTable getTerminology()
        {
            string sql = "select id,name_cn,name_en,definition_cn,definition_en,remark,type from terminology ";
            DataTable dt = new DataTable();
            dt = op.Query(sql);
            return dt;
        }

        public string UpdateTerminology(DataTable dt,int create_by)
        {
            clearAll();
            int success = 0;
            int failure = 0;
            string message_s = string.Empty;
            string message_f = "第";
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow row = dt.Rows[i];
                if (insertItem(row,create_by))
                {
                    success++;
                }
                else
                {
                    failure++;
                    message_f += i.ToString()+",";
                }
            }
            message_s = "名词术语共更新数据"+success.ToString()+"条,共计更新失败" + failure.ToString() + "条。";
            if (failure > 0)
            {
                message_f = message_f.Substring(0, message_f.Length - 1) + "行数据更新失败。";
                return message_s + message_f;
            }
            else
            {
                return message_s;
            }
        }

        private bool clearAll()
        {
            string sql = @"DELETE FROM terminology";
            return op.Execute(sql);
        }

        private bool deleteItem(string key)
        {
            string sql = @"DELETE FROM terminology WHERE name_cn='" + key + "'";
            return op.Execute(sql);
        }

        private bool insertItem(DataRow row,int create_by)
        {
            terminology t = new terminology();
            t.create_by = create_by;
            t.name_cn = row[0].ToString();
            t.name_en = row[1].ToString();
            t.definition_cn = row[2].ToString();
            t.definition_en = row[3].ToString();
            t.type = row[4].ToString();

            string sql = @"INSERT INTO terminology
             (name_cn,name_en,definition_cn,definition_en,type,created_at,viewnum,created_by) VALUES
             ('"+t.name_cn+"','"+t.name_en+"','"+t.definition_cn+"','"+t.definition_en+"','"+
                t.type+"','"+t.create_at+"','"+t.viewnum.ToString()+"','"+t.create_by.ToString()+"')";
            return op.Execute(sql);
            //string sql = @"INSERT INTO terminology
            //(name_cn,name_en,definition_cn,definition_en,type,created_at,viewnum,remark,created_by)
            //VALUES
            //('材料','materials','可以用来制造有用的构件、器件或物品的物质。','','','2010-12-3','0','','1')";
        }

        private class terminology
        {
            public string name_cn,name_en,definition_cn,definition_en,type;
            public int create_by;
            public int viewnum = 0;
            public DateTime create_at = DateTime.Now;
        }
    }
}
